<?php
class ml_model_rdsCount extends ml_model_redis
{
    const PFX_GD_ATT_CNT = 'gdAttC_';
    const PFX_RES_CMT_CNT = 'ComC_';
    const PFX_RES_COLLECT_CNT = 'ColC_';
    const PFX_U_ATT_CNT = 'uAttC_';
    const PFX_U_FEED_CNT = 'uFdC_';
    const PFX_U_CONTENT_CNT = 'uCC_';
    const PFX_U_FOLLOW_CNT = 'uFC_';
    const PFX_U_FANS_CNT = 'uFSC_';
    const PFX_U_SHOP_LIMIT = 'uSPL_';//好店进后台的数量限制,user_shop_publish_limit
    const PFX_U_SHOP_TYPE = 'uST_';//uid属性user_shop_type
    const PFX_A_FEED_CNT = 'aFdC_';
    const PFX_PA_FEED_CNT = 'pAFdC_';
    const PFX_A_REPLY_CNT = 'aRyC_';


    function __construct() {
        if(!$this->init_rds('meila_cnt'))
        return false;
    }


    public function getUserStat($aUid)
    {
        $aKeys = array();
        foreach ($aUid as $uid)
        {
            //$aKeys[] = self::PFX_U_ATT_CNT.$uid;
            //$aKeys[] = self::PFX_U_FEED_CNT.$uid;
            $aKeys[] = self::PFX_U_FOLLOW_CNT.$uid;
            $aKeys[] = self::PFX_U_FANS_CNT.$uid;
            $aKeys[] = self::PFX_U_CONTENT_CNT.$uid;
        }

        $aKeyRe = array(
        //self::PFX_U_ATT_CNT    => 'att_cnt',
        //self::PFX_U_FEED_CNT => 'feed_cnt',
        self::PFX_U_FOLLOW_CNT=> 'follow_cnt',
        self::PFX_U_FANS_CNT=> 'fans_cnt',
        self::PFX_U_CONTENT_CNT=> 'content_cnt',
        );

        $aRs = $this->mget($aKeys);
        foreach ($aRs as $k => $value)
        {
            list($key , $uid) = explode('_' , $aKeys[$k]);

            $list[$uid][strtr($key.'_' , $aKeyRe)] = (int)$value > 0 ? (int)$value : 0;
        }
        return $list;
    }


    /**
     * 获取好店发的宝贝数
     * @param uid
     * @return cnt
     */
    public function getShopCount($uid)
    {
        $key = self::PFX_U_SHOP_LIMIT .$uid;

        $Rs = $this->get($key);
        $this->set_data( $Rs);
        return true;
    }

    /**
     * 获取uid的属性
     * @param uid
     * @return cnt
     */
    public function getShopType($uid)
    {
        $key = self::PFX_U_SHOP_TYPE .$uid;

        $Rs = $this->get($key);
        $this->set_data( $Rs);
        return true;
    }
    /**
     * 如果没有超过限制，数量加1
     * @param uid
     */
    public function incrShopCount($uid)
    {
        $Key = self::PFX_U_SHOP_LIMIT .$uid;

        return $this->incr($Key);
    }


    /**
     * 取宝贝被表态得分
     *
     * @param array $aRid
     * @return array
     */
    public function getAttitudeCount($aRid)
    {
        return true;
        foreach ($aRid as &$rid)
        $aKey[] = self::PFX_GD_ATT_CNT .$rid;

        $aRs = $this->mget($aKey);
        $this->set_data( array_combine($aRid , $aRs) );
        return true;
    }
    /**
     * 增加宝贝被表态数
     *
     * @param string $rid
     * @param int $count
     * @return int //
     */
    public function incrAttitudeCount($rid)
    {
        $rid = self::PFX_GD_ATT_CNT .$rid;
        return $this->incr($rid);
    }


    public function getCommentCount($aRid)
    {
        if(!is_array($aRid) || count($aRid)<1)
            return true;
        foreach ($aRid as &$rid)
        $aKey[] = self::PFX_RES_CMT_CNT .$rid;

        $aRs = $this->mget($aKey);
        if($aRs)
            $this->set_data( array_combine($aRid , $aRs) );
        return true;
    }
    /**
     * 增加评论数
     *
     * @param unknown_type $rid
     * @return unknown
     */
    public function incrCommentCount($rid)
    {
        $rid = self::PFX_RES_CMT_CNT .$rid;
        return $this->incr($rid);
    }
    /**
     * 减少评论数
     *
     * @param unknown_type $rid
     * @return unknown
     */
    public function decrCommentCount($rid)
    {
        $rid = self::PFX_RES_CMT_CNT .$rid;
        return $this->decr($rid);
    }

    /**
     * 增加用户被表态次数
     *
     * @param int $uid
     * @param ubt $count
     * @return int
     */
    public function incrUserAttitudeCount($uid)
    {
        $uid = self::PFX_U_ATT_CNT .$uid;
        return $this->incr($uid);
    }
    /**
     * 取用户被表态次数
     *
     * @param int $uid
     * @return int
     */
    public function getUserAttitudeCount($uid)
    {
        return $this->get(self::PFX_U_ATT_CNT .$uid);
    }



    public function getUserfeedCnt($uid)
    {
        return $this->get(self::PFX_U_FEED_CNT .$uid);
    }
    public function incrUserfeedcnt($uid)
    {
        $uid = self::PFX_U_FEED_CNT .$uid;
        return $this->incr($uid);
    }
    public function decrUserfeedcnt($uid)
    {
        $uid = self::PFX_U_FEED_CNT .$uid;
        return $this->decr($uid);
    }



    public function getUserContentCnt($uid)
    {
        return $this->get(self::PFX_U_CONTENT_CNT .$uid);
    }
    public function incrUserContentcnt($uid)
    {
        $uid = self::PFX_U_CONTENT_CNT .$uid;
        return $this->incr($uid);
    }
    public function decrUserContentcnt($uid)
    {
        $uid = self::PFX_U_CONTENT_CNT .$uid;
        return $this->decr($uid);
    }



    /**
     * 增加关注数
     * @param unknown_type $uid
     */
    public function incrFollowCnt($uid)
    {
        $uid = self::PFX_U_FOLLOW_CNT .$uid;
        return $this->incr($uid);
    }
    /**
     * 减少关注数
     * @param unknown_type $uid
     */
    public function decrFollowCnt($uid)
    {
        $uid = self::PFX_U_FOLLOW_CNT .$uid;
        return $this->decr($uid);

    }
    /**
     * 获取用户关注数
     * @param int $uid
     */
    public function getFollowCnt($uid)
    {
        $uid = self::PFX_U_FOLLOW_CNT .$uid;
        return $this->get($uid);
    }
    
    /**
     *  获取画报分享数 
     */
    public function getAlbumfeedCnt($album_id){
        $key = self::PFX_A_FEED_CNT.$album_id;
        return $this->get($key);
    }
    
    /**
     *  获取画报分享数
     */
    public function getAlbumfeedCnts($album_ids){
        if(!is_array($album_ids))
            return false;
        foreach($album_ids as $album_id){
            $keys[] = self::PFX_A_FEED_CNT.$album_id;
        }
        $album_cnts = $this->mget($keys);
        if(!$album_cnts)
            return false;
        return array_combine($album_ids, $album_cnts);
    }
    
    /**
     *  添加画报分享数
     */
    public function incrAlbumfeedCnt($album_id){
        $key = self::PFX_A_FEED_CNT.$album_id;
        return $this->incr($key);
    }
    /**
     * 减少画报分享数
     */
    public function decrAlbumfeedCnt($album_id){
        $key = self::PFX_A_FEED_CNT.$album_id;
        return $this->decr($key);
    }
    /**
     *  获取画报图片分享数
     */
    public function getPicAlbumfeedCnt($album_id,$pic_id){
        $key = self::PFX_PA_FEED_CNT.$album_id.'_'.$pic_id;
        return $this->get($key);
    }
    
    /**
     *  添加画报图片分享数
     */
    public function incrPicAlbumfeedCnt($album_id,$pic_id){
        $key = self::PFX_PA_FEED_CNT.$album_id.'_'.$pic_id;
        return $this->incr($key);
    }
    /**
     * 减少画报图片分享数
     */
    public function decrPicAlbumfeedCnt($album_id,$pic_id){
        $key = self::PFX_PA_FEED_CNT.$album_id.'_'.$pic_id;
        return $this->decr($key);
    }
    public function getPicAlbumfeedCntByPids($pic_ids){
        if(!is_array($pic_ids))
            return false;
    
        foreach ($pic_ids as $pic_id)
            $keys[] = self::PFX_PA_FEED_CNT.$pic_id;
        
        $aRs = $this->mget($keys);
        $result = array_combine($pic_ids , $aRs);
        return $result;
    }
    /**
     *  获取画报分享回复数
     */
    public function getAlbumreplyCnt($rid){
        $key = self::PFX_A_REPLY_CNT.$rid;
        return $this->get($key);
    }
    
    /**
     *  添加画报分享回复数
     */
    public function incrAlbumreplyCnt($rid){
        $key = self::PFX_A_REPLY_CNT.$rid;
        return $this->incr($key);
    }
    /**
     * 减少画报分享回复数
     */
    public function decrAlbumreplyCnt($rid){
        $key = self::PFX_A_REPLY_CNT.$rid;
        return $this->decr($key);
    }
    /**
     * 增加粉丝数
     * @param int $uid
     */
    public function incrFansCnt($uid)
    {
        $uid = self::PFX_U_FANS_CNT .$uid;
        return $this->incr($uid);
    }
    /**
     * 减少粉丝数
     * @param int $uid
     */
    public function decrFansCnt($uid)
    {
        $uid = self::PFX_U_FANS_CNT .$uid;
        return $this->decr($uid);
    }
    /**
     * 获取用户粉丝数
     * @param int $uid
     */
    public function getFansCnt($uid)
    {
        $uid = self::PFX_U_FANS_CNT .$uid;
        return $this->get($uid);
    }


    /**
     * 增加宝贝转采数
     * @param int $rid
     */
    public function incrColCnt($rid)
    {

        $rid = self::PFX_RES_COLLECT_CNT.$rid;
        return $this->incr($rid);
    }
    /**
     * 减少宝贝转采数
     * @param int  $rid
     */
    public function decrColCnt($rid)
    {

        $rid = self::PFX_RES_COLLECT_CNT.$rid;
        return $this->decr($rid);
    }
    /**
     * 批量获取宝贝的转采数
     * @param int $rids
     */
    public function getColCnt($rids)
    {

        foreach ($rids as &$rid)
        $aKey[] = self::PFX_RES_COLLECT_CNT.$rid;

        $aRs = $this->mget($aKey);
        if(is_array($aRs))
            $this->set_data( array_combine($rids , $aRs) );
        return true;
    }

    /**
     * 批量获取  粉丝数 关注数 内容数 表态数
     * @param array $uids
     */
    public function getCntByUids($uids) {

        //    有点纠结把这段放到 控制器里 做还是在这里。。
        $fans = $follows = $contents = $atts = $nicks = $heads = $list = array();
        foreach($uids as $k => $v) {
            $fans[] = 'uFSC_'.$v;        //    粉丝
            /*     $follows[] = 'uFC_'.$v;        //    关注
            $contents[] = 'uCC_'.$v;        //    内容
            $atts[] = 'uAtt_'.$v;        //    投票    表态
            */
            $nicks[] = 'u2n_'.$v;        //    昵称
            $heads[] = ml_tool_picid::uid2portrait($v);    //头像
        }



        $fansList = $this->oRedis->getMultiple($fans);
        /*         $followList = $this->oRedis->getMultiple($follows);
         $contentList = $this->oRedis->getMultiple($contents);
         $attList = $this->oRedis->getMultiple($atts);
         */
        
        $nicksList = ml_tool_nick::uids2nicks($uids);

        foreach($uids as $k => $v) {
            $list[$v]['nick'] = $nicksList[$k]=== false ? 0 : $nicksList[$k];
            $list[$v]['fansCnt'] = $fansList[$k] === false ? 0 : $fansList[$k];
        }

        return $list;

    }
}